package components.bullets
{
	import capricornus.actor.Actor;
	import capricornus.utils.PointUtils;
	
	import components.bullets.controller.BulletImageController;
	import components.bullets.controller.BulletLineMoveController;
	import components.bullets.controller.BulletMoveController;
	import components.bullets.controller.BulletParabolaMoveController;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.utils.setTimeout;
	
	import flashx.textLayout.formats.FormatValue;
	
	import scene.Scene;
	
	public class Bullet extends Sprite
	{
		private var _beginHeight:int;
		private var _endHeight:int;
		private var _diffHeight:int;
		
		private var _vo:BulletVO;
		
		private var _isActivate:Boolean = false;
		private var _container:Sprite;
		
		private var _imageController:BulletImageController;
		private var _moveController:BulletMoveController;
		
		public function Bullet( vo:BulletVO )
		{
			super();
			_vo = vo;
			init();
		}
		
		private function init():void
		{
			this.mouseChildren = false;
			this.mouseEnabled = false;
			this.visible = false;
			
			_container = new Sprite();
			addChild( _container );
			
			_beginHeight = int(_vo.tower.attribute.height * 0.666);
			_endHeight = int(_vo.demon.attribute.height * 0.6666);
			_diffHeight = _beginHeight - _endHeight;
			
			_container.y = -_beginHeight;
			
			_vo.demon.getWillHurt( _vo.damage );
			
			if( _vo.type==0 )
			{
				_moveController = new BulletLineMoveController( this );
			}
			else if( _vo.type==1 )
			{
				_moveController = new BulletParabolaMoveController( this );
			}
			_imageController = new BulletImageController( this );
			_moveController.setInfo( _endHeight, _diffHeight );
		}
		
		public function destroy():void
		{
			_vo = null;
			deactivate();
			if( parent != null )
			{
				parent.removeChild( this );
			}
			if( _moveController != null )
			{
				_moveController.destroy();
				_moveController = null;
			}
			if( _imageController != null )
			{
				_imageController.destroy();
				_imageController = null;
			}
		}
		
		public function get container():Sprite
		{
			return _container;
		}
		
		public function get vo():BulletVO
		{
			return _vo;
		}
		
		public function activate():void
		{
			if( !_isActivate )
			{
				_isActivate = true;
				BulletManager.instance.addBullet( this );
			}
		}
		
		public function deactivate():void
		{
			if( _isActivate )
			{
				_isActivate = false;
				BulletManager.instance.removeBullet( this );
			}
		}
		
		public function directFrom( from:Array, to:Array ):void
		{
			if( _vo.isAreaHit )
			{
				
			}
			else
			{
				PointUtils.rotatePointToPoint2( _container, from, to );
			}
		}
		
		public function setLocation( x:int, y:int ):void
		{
			this.x = x;
			this.y = y;
		}
		
		public function refresh(time:uint):void
		{
			_imageController.refresh( time );
			_moveController.refresh( time );
		}
	}
}